<?php
//加载配置
include_once 'common/common.function.php';
include_once 'common/database.class.php';

//参数
$verno = intval($_GET['verno']);
$step = intval($_GET['step']);
$sid = intval($_GET['sid']);
//判断参数是否正确
if(!$verno || !$step || !$sid){
	$json['url'] = '';
	$json['msg'] = '请求数据为空';
	$json['sta'] = 'error' ;
	echo json_encode($json);
	exit;
}
//是否是1~3步骤
if( !in_array($step,array(1,2,3)) ){
	$json['url'] = '';
	$json['msg'] = '错误的step参数值:'.$step;
	$json['sta'] = 'error' ;
	echo json_encode($json);
	exit;
}



//执行相应方法操作
switch($step){
	case 1:
		step_1($sid,$verno);
		break;
	case 2:
		step_2($sid,$verno);
		break;
	case 3:
		step_3($sid,$verno);
		break;
	default:
		$json['url'] = '';
		$json['msg'] = '错误的step参数值:'.$step;
		$json['sta'] = 'error' ;
		echo json_encode($json);
		exit;
		break;
}

function get_server($sid){
	$db = DataBase::getInstance();
	$server = $db->get_one(" select * from yabb_sys_server_list where Id='{$sid}' ");
	if(!$server){
		$json['url'] = '';
		$json['msg'] = '找不到url地址';
		$json['sta'] = 'error' ;
		echo json_encode($json);
		exit;
	}	
	if($server['FileNo']==$verno && $server['DbNo']==$verno){
		$json['url'] = '';
		$json['msg'] = '同步完成';
		$json['sta'] = '100%' ;
		echo json_encode($json);
		exit;
	}elseif($server['FileNo']<$verno && $server['DbNo']==$verno){
		
	}elseif($server['FileNo']==$verno && $server['DbNo']<$verno){
		
	}	
	return $server;
}
function get_version($verno){
	$db = DataBase::getInstance();
	$version = $db->get_one(" select * from yabb_sys_ver where Id='{$verno}' ");
	if(!$version){
		$json['url'] = '';
		$json['msg'] = '错误的verno版本';
		$json['sta'] = 'error' ;
		echo json_encode($json);
		exit;
	}
	return $version;
}


function combine_file_contents($file){
	if(!$file){
		return ;
	}
	$filename = str_replace('\\','/',dirname(__FILE__)).'/ver/'.$file['VerNo'].'/'.$file['FilePath'].'/'.$file['FileName'];
	$filecontent = file_get_contents($filename);
	if($filecontent===false){
		return ;
	}	
	$CombineContent='';

	$CombineContent.=chr(strlen($file['Md5'])).$file['Md5'];
	$CombineContent.=short_to_str(strlen($file['FileName'])).$file['FileName'];
	$CombineContent.=short_to_str(strlen($file['FilePath'])).$file['FilePath'];
	$CombineContent.=long_to_str($Info['FileSize']);
	$CombineContent.=long_to_str(strlen($filecontent)).$filecontent;
		
	return $CombineContent;	
}

function recombine_file_contents($CombineContent){
	$SyncFileList=array();
	$i=0;
	$sLen=strlen($CombineContent);
	while($i<$sLen){
		$Tlen=ord($CombineContent[$i]);$i=$i+1;
		$Md5=substr($CombineContent,$i,$Tlen);$i=$i+$Tlen;

		$Tlen=str_to_short(substr($CombineContent,$i,2));$i=$i+2;
		$FileName=substr($CombineContent,$i,$Tlen);$i=$i+$Tlen;

		$Tlen=str_to_short(substr($CombineContent,$i,2));$i=$i+2;
		$FilePath=substr($CombineContent,$i,$Tlen);$i=$i+$Tlen;

		$FileSize=str_to_long(substr($CombineContent,$i,4));$i=$i+4;

		$Tlen=str_to_long(substr($CombineContent,$i,4));$i=$i+4;
		$Content=substr($CombineContent,$i,$Tlen);$i=$i+$Tlen;

		$SyncFileList[]=array(
		'Md5'=>$Md5,
		'FileName'=>$FileName,
		'FilePath'=>$FilePath,
		'FileSize'=>$FileSize,
		'Content'=>$Content,
		);
	}
	return $SyncFileList;
}


function step_1($sid,$verno){	
	$db = DataBase::getInstance();
	$check = $db->get_one(" select * from yabb_sys_sync_file where ServerId='{$sid}' and VerNo='{$verno}' ");
	if(!$check){
		//整理版本文件
		$sql = " INSERT  into yabb_sys_sync_file (`ServerId`,`VerNo`,`FileId`) ( select {$sid},{$verno},Id from yabb_sys_ver_file) ";
		$db->execute($sql);			
	}
	$json['url'] = 'http://www.y.com/version_sync_ajax_send.php?verno='.$verno.'&step=2&sid='.$sid;
	$json['msg'] = '整理版本';
	$json['sta'] = 'check' ;
	echo json_encode($json);
	exit;	
}

function step_2($sid,$verno){
	$db = DataBase::getInstance();
	$file = $db->get_one(" select * from yabb_sys_ver_file where Id=(select FileId from yabb_sys_sync_file where VerNo='{$verno}' and ServerId='{$sid}' and `Status`='0' order by Id asc limit 1)  ");
	if($file){
		$server = $db->get_one(" select * from yabb_sys_server_list where Id='{$sid}' ");
		include_once 'common/httpdown.class.php';
		$httpdown = new httpdown();
		$url = $server['ServerHost'].'sync_recieve_file.php';
		$filename = str_replace('\\','/',dirname(__FILE__)).'/ver/'.$verno.'/'.$file['FilePath'].'/'.$file['FileName'];
		$filecontent = combine_file_contents($file);
		$httpdown->add_filecontents('PackFile', $file['Md5'], $filecontent);		
		$httpdown->connect($url);
		$s = $httpdown->getraw();			
		if($s=='ok'){
			//更新状态
			$sql = "update yabb_sys_sync_file set `Status`='1' where VerNo='{$verno}' and ServerId='{$sid}' and FileId='".$file['Id']."' ";
			$db->execute($sql);
			//统计值
			$num_ago = $db->get_one(" select COUNT(*) Num from yabb_sys_sync_file where `Status`='1' ");
			$num_all = $db->get_one(" select COUNT(*) Num from yabb_sys_sync_file ");
			$num = intval($_GET['num'])+1;
			$json['url'] = 'http://www.y.com/version_sync_ajax_send.php?verno='.$verno.'&step=2&sid='.$sid.'&num='.$num;
			$json['msg'] = $file['FileName'];//.' 文件同步成功';
			$json['sta'] = intval($num_ago['Num']).'/'.intval($num_all['Num']);
			echo json_encode($json);
			exit;			
		}else{
			$num = intval($_GET['num'])+1;
			if($num>=5){
				$json['url'] = '';
				$json['msg'] = ' 错误信息:'.$s;
				$json['sta'] = 'error';
				echo json_encode($json);
				exit;
			}
			//统计值
			$num_ago = $db->get_one(" select COUNT(*) Num from yabb_sys_sync_file where `Status`='1' and  ServerId='{$sid}' and VerNo='{$verno}' ");
			$num_all = $db->get_one(" select COUNT(*) Num from yabb_sys_sync_file where ServerId='{$sid}' and VerNo='{$verno}' ");
			$json['url'] = 'http://www.y.com/version_sync_ajax_send.php?verno='.$verno.'&step=2&sid='.$sid.'&num='.$num;
			$json['msg'] = $file['FileName'].' 文件同步出错:'.$s;
			$json['sta'] = intval($num_ago['Num']).'/'.intval($num_all['Num']);
			echo json_encode($json);
			exit;
		}
	}else{
		$json['url'] = 'http://www.y.com/version_sync_ajax_send.php?verno='.$verno.'&step=3&sid='.$sid;
		$json['msg'] = '校验文件中';
		$json['sta'] = 'check';
		echo json_encode($json);
		exit;	
	}	
}

function step_3($sid,$verno){
	$json['url'] = '';
	$json['msg'] = '校验文件中';
	$json['sta'] = 'check_100';
	echo json_encode($json);
	exit;
}











